package com.example.appframework.login.loginmethod.password;

import androidx.lifecycle.ViewModelProviders;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.example.appframework.MainActivity;
import com.example.appframework.R;
import com.example.appframework.login.LoginMainActivity;
import com.example.appframework.mysql.DBConnection;

public class PasswordLoginFragment extends Fragment {

    private PasswordLoginViewModel mViewModel;
    EditText editTextUsername, editTextPassword;

    public static PasswordLoginFragment newInstance() {
        return new PasswordLoginFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.password_login_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(PasswordLoginViewModel.class);
        // TODO: Use the ViewModel

        editTextUsername = getView().findViewById(R.id.editTextUsername);
        editTextPassword = getView().findViewById(R.id.editTextPassword);

        getView().findViewById(R.id.passwordConfirmButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String username = editTextUsername.getText().toString();
                final String password = editTextPassword.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)) {
                    Toast.makeText(getContext(), "Can't be empty!",Toast.LENGTH_SHORT).show();
                    Log.i("show", "empty!");
                }
                else{
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if(DBConnection.query(username,password)){
                                Log.i("show", "Right");
                                Log.i("show", username);
                                Looper.prepare();
                                Toast.makeText(getContext(), "Trying to login!", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(getActivity(), MainActivity.class);
                                intent.putExtra("username", username);
                                intent.putExtra("order", 1);
                                startActivity(intent);
                                Looper.loop();
                            }
                            else{
                                Looper.prepare();
                                Toast.makeText(getContext(), "Your information is wrong!", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                                Log.i("show", "failed!");
                            }
                        }
                    }).start();
                }
            }
        });
    }

}